from typing import Dict, Any, Optional
from .storage_base import BaseStorageService, StorageProvider
from .minio_storage import MinIOStorageService

class StorageServiceFactory:
    """存储服务工厂"""
    
    _service_classes = {
        StorageProvider.MINIO: MinIOStorageService,
        # 可以添加其他存储服务商
        # StorageProvider.AWS_S3: S3StorageService,
        # StorageProvider.ALIYUN_OSS: OSSStorageService,
    }
    
    @classmethod
    def create_service(cls, provider: StorageProvider, config: Dict[str, Any]) -> BaseStorageService:
        """创建存储服务实例"""
        service_class = cls._service_classes.get(provider)
        if not service_class:
            raise ValueError(f"不支持的存储服务提供商: {provider}")
        
        return service_class(config)

class StorageManager:
    """存储服务管理器"""
    
    def __init__(self):
        self.services: Dict[StorageProvider, BaseStorageService] = {}
        self.default_service: Optional[BaseStorageService] = None
    
    def register_service(self, provider: StorageProvider, config: Dict[str, Any], is_default: bool = False):
        """注册存储服务"""
        service = StorageServiceFactory.create_service(provider, config)
        self.services[provider] = service
        
        if is_default or not self.default_service:
            self.default_service = service
    
    def get_service(self, provider: Optional[StorageProvider] = None) -> Optional[BaseStorageService]:
        """获取存储服务"""
        if provider and provider in self.services:
            return self.services[provider]
        
        return self.default_service
    
    def get_all_services(self) -> Dict[StorageProvider, BaseStorageService]:
        """获取所有存储服务"""
        return self.services.copy()

# 全局存储管理器实例
storage_manager = StorageManager()